﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCL.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 巡检路线管理
    /// </summary>
    /// Title:ZMCL91
    /// Service:ServiceZMCL91
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-6-30  高强   [创建]
    ///  
    public partial class ZMCL91 : EFViewCmes
    {
        private string defaultFrequencyType;//默认的频率类型
        private string pathCode;//巡检路线编号
        private EIBlock frequencyUnitsBlock;//频率类型block
        private string actionType;//处理类型

        public ZMCL91()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            frequencyUnitsBlock = initData.GetBlock("cmes.cl.frequencyUnits");//频率单位block
            defaultFrequencyType = initData.Get("defaultFrequencyType").ToString();//默认频率单位
            pathCode = initData.Get("fdPathCode").ToString();//获得巡检路线编号
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            _view.ViewType = "tree";
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "巡检路线管理-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出修改页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "巡检路线管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 点击启用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStartButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "modifyStart";
            base.OnModifyButtonClick("modifyStart", "启用", sender, e);
        }

        /// <summary>
        /// 点击停用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStopButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "modifyStop";
            base.OnModifyButtonClick("modifyStop", "停用", sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            InsertOrUpdateSercurityPath insertOrUpdateSercurityPath = new InsertOrUpdateSercurityPath(_dialogType);//构造一个新的修改页面
            insertOrUpdateSercurityPath.ViewDataChanged += new EventHandler<ViewDataChangedEventArgs>(insertOrUpdateSercurityPath_ViewDataChanged);//添加监听
            insertOrUpdateSercurityPath.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(frequencyUnitsBlock);
            if ("insert".Equals(_dialogType))
            {
                insertOrUpdateSercurityPath.fdFrequencyUnits.Value = defaultFrequencyType;//传递默认频率的类型
                insertOrUpdateSercurityPath.fdPathCode.Value = pathCode;//传递巡检路线的编号
            }
            insertOrUpdateSercurityPath.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = insertOrUpdateSercurityPath;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 给弹出窗口页面添加一个监听，为了获取弹出窗口中值改变后的新值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void insertOrUpdateSercurityPath_ViewDataChanged(object sender, ViewDataChangedEventArgs e)
        {
            pathCode = e.NewData.ToString();//获得重新生成的巡检路线编号
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
                this.UpdateHomePageData(new EIInfo());//发布数据改变
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 停用前的判断
        /// </summary>
        /// <param name="rowList"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            bool Flag = true;
            if (this.actionType == "modifyStop")
            {
                foreach (var row in _eIRows)
                {
                    if (row["fdNoteCode"] != null && !(row["fdNoteCode"].ToString().Equals("")))
                    {
                        base.OpenEFWindowAlert("选择停用的巡检路线已经被巡检工单引用,请先在巡检工单中取消对本巡检路线的引用,再停用该巡检路线!", true);
                        Flag = false;
                        break;
                    }
                }
            }
            this.actionType = null;
            return Flag;
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }
    }
}
